﻿using HandyControl.Controls;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Walterlv.Demo.Interop;
using MessageBox = System.Windows.MessageBox;
using Window = System.Windows.Window;

namespace 倒计时
{
    /// <summary>
    /// Setting.xaml 的交互逻辑
    /// </summary>
    public partial class Setting : Window
    {
        private JObject root = JObject.Parse(File.ReadAllText("App.json"));
        public Setting()
        {
            InitializeComponent();
            WindowBlur.SetIsEnabled(this, true);
            
            if (root["colormode"].ToString().Equals("dark")) nextcolor.Content = "切换成白天模式";
            else nextcolor.Content = "切换成黑夜模式";
            Reload();
            GC.Collect();
        }

        private void Check_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WebClient w1 = new WebClient();
                JObject root = JObject.Parse(Encoding.UTF8.GetString(w1.DownloadData("https://fbtstudio.gitee.io/webframwork/fstore/update.json")));
                Process.Start(root["link"].ToString());
                Growl.SuccessGlobal("请在打开的网站下载最新版本并且替换程序");
            }
            catch { }
        }

        public delegate void playmusic(bool light);
        public static event playmusic Change;
        public static event playmusic Add;
        public static event playmusic TopMost1;

        private void Nextcolor_Click(object sender, RoutedEventArgs e)
        {
            if (root["colormode"].ToString().Equals("dark"))
            {
                nextcolor.Content = "切换成黑夜模式";
                root["colormode"] = "light";
                Change(true);
            }
            else
            {
                nextcolor.Content = "切换成白天模式";
                root["colormode"] = "dark";
                Change(false);
            }
            File.WriteAllText("App.json", root.ToString());
            GC.Collect();
            Growl.SuccessGlobal("切换成功");
        }

        private void Reload()
        {
            foreach (JObject jObject in JArray.Parse(root["items"].ToString()))
            {
                try
                {
                    choose.Items.Add(jObject["title"].ToString());
                    GC.Collect();
                }
                catch (Exception ex)
                {
                    if (root["debug"].ToString().Equals("true")) Growl.ErrorGlobal("程序出现错误\n" + ex.ToString());
                    else Growl.ErrorGlobal("程序出现错误\n" + ex.Message);
                    GC.Collect();
                }
            }
            choose.SelectedIndex = 0;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!date.SelectedDate.Equals(null) && title.Text.Length > 1)
            {
                if ((DateTime.Parse(date.SelectedDate.ToString()) - DateTime.Now.Date).Days > 0)
                {
                    JArray items = JArray.Parse(root["items"].ToString());
                    JObject dataobj = new JObject();
                    dataobj.Add("title", title.Text);
                    dataobj.Add("time", DateTime.Parse(date.SelectedDate.ToString()).ToShortDateString());
                    items.Add(dataobj);
                    root["items"] = items;
                    File.WriteAllText("App.json", root.ToString());
                    GC.Collect();
                    Growl.SuccessGlobal("添加成功!");
                    Add(true);
                }
                else Growl.ErrorGlobal("时间小于一天，不能添加哦");
            }
            else Growl.ErrorGlobal("您似乎没有按照格式填写哦!");
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                JArray items = JArray.Parse(root["items"].ToString());
                items.Remove(items[choose.SelectedIndex]);
                choose.Items.Remove(choose.Items[choose.SelectedIndex]);
                choose.SelectedIndex = 0;
                root["items"] = items;
                File.WriteAllText("App.json", root.ToString());
                GC.Collect();
                Growl.SuccessGlobal("移除成功!");
                Add(true);
            }
            catch { }
        }

        private void Topmost_Click(object sender, RoutedEventArgs e)
        {
            TopMost1(true);
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
